/*
 * Project Beknyou
 * Copyright (c) 2010-2011 Saint Paul College, All Rights Reserved
 * Redistributions in source code form must reproduce the above
 * Copyright and this condition.
 * The contents of this file are subject to the GNU General Public
 * License, Version 2 (the "License"); you may not use this file
 * except in compliance with the License. A copy of the License is
 * available at http://www.opensource.org/licenses/gpl-license.php.
 */
package com.benkyou.util;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;


import javax.sql.DataSource;
/**
 * 
 * @author Ralph
 */

//DataSource has been implemented on its own vs. using a third party driver 
public class DataSourceObject implements DataSource {
        Connection connection = null;
        private String[] dBName = {"TestTable"};
        private int database;

        @Override
        public Connection getConnection() throws SQLException {
                if (connection != null) {
                        System.out.println("Connection failed");
                } else {
                        connection = DriverManager.getConnection(
                                        "jdbc:mysql://192.168.10.13:3306/student", "root", "root");
                }
                return connection;
        }

        @Override
        public Connection getConnection(String username, String password)
                        throws SQLException {
                // TODO Auto-generated method stub
                if (connection != null) {
                        System.out.println("Connection failed");
                } else {
                        connection = DriverManager.getConnection(
                                        "jdbc:mysql://localhost:3306/"+getDBName(), username,
                                        password);
                }
                System.out.println("Connection succeeded");
                return connection;
        }
        
             public Connection getConnection(String database, String username, String password)
                        throws SQLException {
                // TODO Auto-generated method stub
                if (connection != null) {
                        System.out.println("Connection failed");
                } else {
                        connection = DriverManager.getConnection(
                                        "jdbc:mysql://localhost:3306/"+database, username,
                                        password);
                }
                System.out.println("Connection succeeded");
                return connection;
        }

        @Override
        public PrintWriter getLogWriter() throws SQLException {

                return null;
        }

        @Override
        public int getLoginTimeout() throws SQLException {
                // TODO Auto-generated method stub
                return 0;
        }

        @Override
        public void setLogWriter(PrintWriter out) throws SQLException {
                // TODO Auto-generated method stub
        }

        @Override
        public void setLoginTimeout(int seconds) throws SQLException {
            
                // TODO Auto-generated method stub
        }

    public <T> T unwrap(Class<T> iface) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public void setDBName(int x) {
        if (x >= 0 && x <= dBName.length - 1) {
        database = x;
        }
    }
    
    public String getDBName() {
   
        return dBName[database];
    
    }

    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}


